﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EventRegistration;


public partial class NewOrderForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CurrentData.RegistrationInfo.Completed) //If user not login, redirect to login page
            {
                Response.Redirect("Register/Login.aspx");
            }
        }

        protected void ButtonCreateNewOrder_Click(object sender, EventArgs e)
        {
            string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();
            using (SqlConnection con = new SqlConnection(url))
            {
                try
                {
                    string sql = "insert into OrderScheduling (ProductID,SupplierID,Subject,Description,Location,Quantity,StartDate,EndDate) " +
                                 "values(@product,@supplier,@subject,@description,@location,@quantity,@start,@end)";

                    con.Open();
                    SqlCommand cmd = new SqlCommand(sql, con);

                    #region Get data from input then assign them to the query command
                    SqlParameter param_productID = new SqlParameter("@product", System.Data.SqlDbType.NChar, 40);
                    param_productID.Value = productDropDownList.SelectedItem.Value;
                    cmd.Parameters.Add(param_productID);

                    SqlParameter param_supplierID = new SqlParameter("@supplier", System.Data.SqlDbType.NChar, 30);
                    param_supplierID.Value = supplierDropDownList.SelectedItem.Value;
                    cmd.Parameters.Add(param_supplierID);

                    SqlParameter param_subject = new SqlParameter("@subject", System.Data.SqlDbType.NChar, 30);
                    param_subject.Value = subjectTextbox.Text;
                    cmd.Parameters.Add(param_subject);

                    SqlParameter param_description = new SqlParameter("@description", System.Data.SqlDbType.NChar, 60);
                    param_description.Value = descriptionTextbox.Text;
                    cmd.Parameters.Add(param_description);

                    SqlParameter param_location = new SqlParameter("@location", System.Data.SqlDbType.NChar, 15);
                    param_location.Value = locationTextbox.Text;
                    cmd.Parameters.Add(param_location);

                    SqlParameter param_quantity = new SqlParameter("@quantity", System.Data.SqlDbType.Int);
                    param_quantity.Value = quantityTextbox.Text;
                    cmd.Parameters.Add(param_quantity);

                    SqlParameter param_startDate = new SqlParameter("@start", System.Data.SqlDbType.Date);
                    param_startDate.Value = startDateDateEdit.Value;
                    cmd.Parameters.Add(param_startDate);

                    SqlParameter param_endDate = new SqlParameter("@end", System.Data.SqlDbType.Date);
                    param_endDate.Value = endDateDateEdit.Value;
                    cmd.Parameters.Add(param_endDate);
                    #endregion
                    cmd.ExecuteNonQuery(); //Execute the command
                    statusLabel.Text = "Successfully!";
                }
                catch (Exception ex)
                {
                    statusLabel.Text = ex.Message; //Show the error message if has
                }
                finally
                {
                    con.Close();
                }
            }
        }

        protected void backButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("Schedule.aspx");     //Redirect to Schedule page
        }
}

